<?php
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/admindetails.php';
include_once '../../model/email.php';
include_once("../../model/users.php");
require_once '../../model/state.php';
require_once '../../redirect.php';

checkUserLogin();
## Create Objects
/*******************************/
$adminObj = new Model_AdminDetails();
$emailObj = new  Model_Email();
$userObj = new Model_Users();
$stateObj = new Model_State();
/*******************************/

checkUserLogin();

$userId = $_SESSION['getbids_id'];
$userDetArray = $userObj->getUserDetailsByUserId($userId);
$comapnyname = str_replace(" ","_",strtolower($userDetArray['companyname']));
$userDetArray['newcomapnyname'] =$comapnyname;
//print_R($userDetArray);die();
$smarty->assign('userDetArray',$userDetArray);

$getpostalcodedetail = $stateObj->getPostalcodeExits($userDetArray['workarea']);
$smarty->assign('getpostalcodedetail',$getpostalcodedetail);


## Fetch all active states
$stateArray = $stateObj->getAllStateWithActiveStatus();
//echo "<pre>"; print_r($stateArray); exit;
$smarty->assign('stateArray', $stateArray);


if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email'])) 
{
	extract($_POST);
	//print_r($_POST);
	$useremailCnt = $userObj->chkUserEmailExist($_POST["email"],$userId);
	$userEmailCount = count($useremailCnt);
	if($userEmailCount<=0)
	{
			$userEditArray = array();
			$userEditArray["first_name"] 	= return_post_value($_POST['fname']);
			$userEditArray["last_name"]		= return_post_value($_POST['lname']);
			$userEditArray["phone_number"]	= return_post_value($_POST['mobno']);
			$userEditArray["email"] 		= return_post_value($_POST['email']);
			$userEditArray["username"] 		= return_post_value($_POST['email']);
			$userEditArray['postalcode'] 	= return_post_value($_POST['postcode']);
			$userEditArray['state_id'] 	= return_post_value($_POST['state_id']);
			$userEditArray['companyname'] 	= return_post_value($_POST['companyname']);
			$userEditArray['companydescription'] 	= return_post_value($_POST['companydescription']);
			$userEditArray['about_me'] 	= return_post_value($_POST['about_me']);
			$userEditArray['workhistory'] 	= return_post_value($_POST['workhistory']);
			$userEditArray['qual_accred'] 	= return_post_value($_POST['qual_accred']);
			$userEditArray["modified_date"] = getCurDate();


			$userEditArray["business_name"]	= return_post_value($_POST['business_name']);
			$userEditArray["industry"] 		= return_post_value($_POST['industry']);
			$userEditArray["abn"] 		= return_post_value($_POST['abn']);
			$userEditArray['acn'] 	= return_post_value($_POST['acn']);
			
			$err = false;
			if($_FILES['companylogo']['name'] != '')
			{
				$size =	getimagesize($_FILES['companylogo']['tmp_name']);
				if($size[0] >= COMPANYIMAGEWIDTH && $size[1] >= COMPANYIMAGEHEIGHT) 
				{   
					@unlink('../../uploads/company/'.$userDetArray['user_avatar']);
					$path = '../../uploads/company/'; 
					$imageName    = $_FILES['companylogo']['name']; 
	
					$arrImageName = explode(".",$imageName);
					$position     = count($arrImageName);
					
					$fileName     = date('Ymdhis').".".$arrImageName[$position-1]; 
					@copy($_FILES['companylogo']['tmp_name'], $path.$fileName);
					
					$userEditArray['companylogo']	= $fileName;
	
				}
				else
				{
				   $err = true;				   
				}
			}
			if($err == false)
			{
				$userObj->editUserValueById($userEditArray,$userId);			
				$_SESSION['msg'] = "<div class='success_msg'><span>Profile updated successfully.</span></div>";
				$comapnyname = str_replace(" ","_",strtolower($userEditArray['companyname']));
				if($_SESSION['user_type'] == '4')
					redirect_to_link(SITE_URL.'/profile/'.$comapnyname);
				else
					redirect_to_link(SITE_URL.'/myaccount/');	
				exit;
			}
			else
			{
					$_SESSION['msg'] = "<div class='error_msg'><span>".COMPANYIMAGE_WIDTHHEIGHT."</span></div>";
			}
		
	 }
	 else
	 {
		 $_SESSION['msg'] = "<div class='error_msg'><span>Email already exists.</span></div>";
	 }
}


if($_SESSION['msg'] != ''){
	$smarty->assign("msg", $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

$active = "myaccount";
$smarty->assign("active", $active);
$subactive ="editprofile";
$smarty->assign("subactive", $subactive);
$smarty->assign('mainmenu', '1');
$smarty->display(TEMPLATEDIR . '/modules/myaccount/editprofile.tpl');
?>